package com.lyun.controller;

import com.lyun.bo.GetAllSmallVideosBo;
import com.lyun.common.BaseContext;
import com.lyun.common.OssTemplate;
import com.lyun.pojo.SmallVideo;
import com.lyun.service.SmallVideoService;
import com.lyun.service.VideoConcernService;
import com.lyun.vo.PageVO;
import com.lyun.vo.SmallVideosVo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {

    @Resource
    private OssTemplate ossTemplate;

    @Resource
    private SmallVideoService smallVideoService;

    @Resource
    private VideoConcernService videoConcernService;

    /**
     * APP-发布小视频
     *
     * @param videoThumbnail 视频缩略图
     * @param videoFile      视频文件
     */
    @PostMapping
    public ResponseEntity<String> publishSmallVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        Long id = BaseContext.getCurrentId();
        //上传缩略图
        String upload = ossTemplate.upload(videoThumbnail);

        //上传视频文件
        String video = ossTemplate.uploadVideo(videoFile);

        SmallVideo smallVideo = new SmallVideo();
        smallVideo.setUserId(id);
        smallVideo.setVideoFile(video);
        smallVideo.setVideoThumbnail(upload);
        smallVideo.setCreateTime(LocalDateTime.now());
        smallVideo.setAuditStatus(0);
        smallVideoService.save(smallVideo);

        return ResponseEntity.ok("发布成功");
    }

    /**
     * APP-小视频列表
     *
     * @param page     页码
     * @param pageSize 每页显示条数
     * @return
     */
    @GetMapping
    public PageVO<SmallVideosVo> getAll(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                        @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {

        GetAllSmallVideosBo bo = new GetAllSmallVideosBo();
        bo.setId(BaseContext.getCurrentId());
        bo.setPage(page);
        bo.setPageSize(pageSize);

        return smallVideoService.getAllSmallVideoList(bo);
    }

    /**
     * APP-视频用户-关注
     *
     * @param id 关注的用户id
     */
    @PostMapping("/{id}/userFocus")
    public void userFocus(@PathVariable("id") Long id) {
        Long selfId = BaseContext.getCurrentId();
        videoConcernService.saveVideoConcern(selfId, id);
    }

    /**
     * APP-视频用户-取消关注
     *
     * @param id 取消关注的用户id
     */
    @PostMapping("/{id}/userUnFocus")
    public void userUnFocus(@PathVariable("id") Long id) {
        Long selfId = BaseContext.getCurrentId();
        videoConcernService.removeByUserIdAndSelfId(selfId, id);
    }
}
